<?php
	defined('RUN') or die('No access');

	class ProductController
	{
		public function __construct()
		{
			if (!Auth::check('acp'))
			{
				Url::redirect('admin/auth/login');
			}
		}
		
		public function index($idcate=null)
		{
			$data = Model::get('admin')->getLayoutData();
			$fields='`id`,`name`,`images`,`sort`';
			$data['product'] = Model::get('product')->getProduct($fields,$idcate);
			if (Session::getFlash('notification'))
			{
				$data['notification']=Session::getFlash('notification');
			}
			$data['content_file'] = 'product/view.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function add($id)
		{
			$data = Model::get('admin')->getLayoutData();
			if (Input::post('action') == 'add' && Security::checkToken('token_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('txtName')))
				{
					Upload::setDestDir('uploads' . DS . 'products');
					Upload::setFileField('images');
					$images = Upload::uploadFile();
                    
                    $birth=explode(".",Input::post('date_start'));
    				$date_start=$birth[2].'-'.$birth[0].'-'.$birth[1];
                    
                    $birth=explode(".",Input::post('date_end'));
    				$date_end=$birth[2].'-'.$birth[0].'-'.$birth[1];
                    
					$data = array(
						'name' 			=> strip_tags(Input::post('txtName')),
						'keywords' 		=> strip_tags(Input::post('txtKeys')),
						'description' 	=> strip_tags(Input::post('txtDesc')),
						'category'    	=> Input::post('slcCategory'),
						'content_short'	=> strip_tags(Input::post('txtShort')),
						'content' 		=> Input::post('txtContent'),
						'images' 		=> ($images == FALSE ? '' : $images),
                        'price' 		=> (int)(Input::post('price')),
                        'per' 	        => (int)(Input::post('per')),
                        'date_start' 	=> $date_start,
                        'date_end' 	    => $date_end,
						'sort' 			=> (int)(Input::post('txtSort')),
						'status'    	=> Input::post('slcStatus'),
						'updated'    	=> date('Y-m-d h:m:s'),
						'company' 			=> strip_tags(Input::post('company')),
						'address' 		=> strip_tags(Input::post('address')),
						'phone' 	=> strip_tags(Input::post('phone')),				
					);
					
					if (Model::get('product')->add($data))
					{
						Session::setFlash('notification', array('type' => 'green', 'msg' => 'Thêm thành công !'));
						Url::redirect('admin/product/index/'.$id);
					}else{
						Session::setFlash('notification', array('type' => 'red', 'msg' => 'Thêm không thành công !'));
						Url::redirect('admin/product/index/'.$id);
					}
				}

				$data['failed'] = TRUE;
			}
            $data['categories'] = Model::get('product_category')->getCategoriesAll();         
			$data['action'] = 'add';
			$data['content_file'] = 'product/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
        
        public function update($id)
		{
			$data = Model::get('admin')->getLayoutData();
            $data['product'] = Model::get('product')->getById($id);
			$category=$data['product']['category'];
			if (Input::post('action') == 'update' && Security::checkToken('token_form', Input::post('token')))
			{
				Upload::setDestDir('uploads' . DS . 'products');
				Upload::setFileField('images');
				$images = Upload::uploadFile();
				if($images=='')
					$images=$data['product']['images'];
				else{
					unlink('.'.$data['product']['images']);	
				}
                
                $birth=explode(".",Input::post('date_start'));
				$date_start=$birth[2].'-'.$birth[0].'-'.$birth[1];
                
                $birth=explode(".",Input::post('date_end'));
				$date_end=$birth[2].'-'.$birth[0].'-'.$birth[1];
                
				if (Validator::checkRequired(Input::post(), array('txtName')))
				{
					$data = array(
						'name' 			=> strip_tags(Input::post('txtName')),
						'keywords' 		=> strip_tags(Input::post('txtKeys')),
						'description' 	=> strip_tags(Input::post('txtDesc')),
						'category'    	=> Input::post('slcCategory'),
						'content_short'	=> strip_tags(Input::post('txtShort')),
						'content' 		=> Input::post('txtContent'),
						'images' 		=> $images,
                        'price' 		=> (int)(Input::post('price')),
                        'per' 	        => (int)(Input::post('per')),
                        'date_start' 	=> $date_start,
                        'date_end' 	    => $date_end,
						'sort' 			=> (int)(Input::post('txtSort')),
						'status'    	=> Input::post('slcStatus'),
						'updated'    	=> date('Y-m-d h:m:s'),
						'company' 			=> strip_tags(Input::post('company')),
						'address' 		=> strip_tags(Input::post('address')),
						'phone' 	=> strip_tags(Input::post('phone')),					
					);
					if (Model::get('product')->update($data, $id))
					{
						Session::setFlash('notification', array('type' => 'green', 'msg' => 'Chỉnh sửa thành công !'));
						Url::redirect('admin/product/index/'.$category);
					}else{
						Session::setFlash('notification', array('type' => 'red', 'msg' => 'Chỉnh không sửa thành công !'));
						Url::redirect('admin/product/index/'.$category);
					}
				}

				$data['failed'] = TRUE;
			}
            $data['categories'] = Model::get('product_category')->getCategoriesAll();
			$data['action'] = 'update';
			$data['content_file'] = 'product/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}                

		public function delete($id, $confirm = 'no')
		{
			$data = Model::get('admin')->getLayoutData();
			$data['products'] = Model::get('product')->getById($id);
			if ($confirm == 'yes')
			{     
                if (Model::get('product')->delete($id) && unlink('.'.$data['products']['images']))
				{
					Session::setFlash('notification', array('type' => 'green', 'msg' => 'Xoá thành công !'));
					Url::redirect('admin/product/index/'.$data['products']['category']);
				}
			}

			$data['content_file'] = 'product/delete.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
	}

